<?php
namespace Site\Controller;
class SiteController extends CommonController {
    public function index() {
        $this->assign($this->getPageList(M('Site'),array()));
        $this->display();
    }

    public function add() {
        if(IS_POST) {
            D('Site','Mysql')->addSite(I('post.'));
            $this->success('网站添加成功','/site');
        }else {
            $this->display();
        }
    }

    public function update() {
        if(IS_POST) {
            D('Site','Mysql')->updateSite(I('post.'));
            $this->success('网站修改成功','/site');
        }else {
            $id = I('get.id');
            verify($id,'isID','非法网站ID');

            $siteInfo = M('Site')->find($id);
            verify($siteInfo,'notEmpty','网站不存在');

            $this->assign('siteInfo',$siteInfo);
            $this->display();
        }
    }

    public function change() {
        $sid = I('get.sid');
        verify($sid,'notEmpty','网站ID为空');
        verify($sid,'isID','网站ID为空');

        $siteInfo = M('Site')->find($sid);
        verify($siteInfo,'notEmpty','网站不存在');

        session('sid',$sid);
        $this->success('网站切换成功');
    }

    public function cache() {
        $sid = I('get.id',0,'intval');
        verify($sid,'isID','非法网站ID');

        // 网站信息
        $siteInfo = M('Site')->find($sid);
        verify($siteInfo,'notEmpty','网站不存在');

        D('Site','Mongo')->updateSite($sid);

        $this->success('网站《' . $siteInfo['title'] . '》缓存更新成功');
    }
}
?>
